<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-toolbar color="primary">
  <h2 translate>{{ title }}</h2>
  <span class="flex flex-1"></span>
  <div [tb-help]="'connection'"></div>
  <button mat-icon-button
    (click)="cancel()"
    type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
@if (isLoading$ | async) {
  <mat-progress-bar color="warn" mode="indeterminate">
  </mat-progress-bar>
}
<div mat-dialog-content>
  <mat-horizontal-stepper #addConnectionWizardStepper
    [linear]="false"
    [labelPosition]="(stepperLabelPosition | async)"
    [orientation]="(stepperOrientation | async)"
    (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="connectionFormGroup">
      <form [formGroup]="connectionFormGroup" #conFormGroup="ngForm">
        <ng-template matStepLabel>{{ 'ws-client.connections.connection-details' | translate}}</ng-template>
        <mat-form-field class="mat-block">
          <mat-label translate>ws-client.connections.name</mat-label>
          <input matInput formControlName="name">
          <mat-icon matSuffix
                    matTooltipClass="tb-error-tooltip"
                    matTooltip="{{ 'mqtt-client-credentials.name-required' | translate }}"
                    [class.!hidden]="!connectionFormGroup.get('name').hasError('required')"
                    class="tb-error">
            warning
          </mat-icon>
        </mat-form-field>

        <section class="flex flex-col gap-2">
          <tb-toggle-header [style]="{maxWidth: 'fit-content', alignSelf: 'center'}"
            (valueChange)="onAddressProtocolChange($event)"
            [value]="addressProtocol"
            name="addressProtocol"
            useSelectOnMdLg="false"
            appearance="fill">
            <tb-toggle-option [value]="wsAddressProtocolType.WS">
              {{ 'admin.settings.ws' | translate }}
            </tb-toggle-option>
            <tb-toggle-option [value]="wsAddressProtocolType.WSS">
              {{ 'admin.settings.wss' | translate }}
            </tb-toggle-option>
          </tb-toggle-header>
          <mat-form-field class="mat-block" subscriptSizing="fixed" style="margin-bottom: 0">
            <mat-label translate>ws-client.connections.url</mat-label>
            <input matInput formControlName="url">
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'ws-client.connections.url-required' | translate }}"
                      [class.!hidden]="!connectionFormGroup.get('url').hasError('required')"
                      class="tb-error">
              warning
            </mat-icon>
            <mat-icon matSuffix style="color: #ff9a00"
              [class.!hidden]="!displayUrlWarning"
              [matTooltip]="'ws-client.connections.url-warning' | translate">
              warning
            </mat-icon>
          </mat-form-field>
          @if (addressProtocol === wsAddressProtocolType.WSS) {
            <section class="flex flex-row items-center gap-2" style="padding: 0 0 12px 0">
              <mat-slide-toggle formControlName="rejectUnauthorized">
                <span translate>ws-client.connections.reject-unauthorized</span>
              </mat-slide-toggle>
              <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
                [matTooltip]="'ws-client.connections.reject-unauthorized-hint' | translate"
                matTooltipPosition="right">
              info</mat-icon>
            </section>
          }
        </section>

        <section class="flex flex-col">
          <div class="flex flex-col" style="margin: 8px 0 16px 0">
            <tb-toggle-header #credentialsToggle
              [style]="{maxWidth: 'fit-content', alignSelf: 'center'}"
              (valueChange)="onCredentialsGeneratorChange($event)"
              [value]="credentialsGeneratorType"
              name="CredentialsGeneratorType"
              useSelectOnMdLg="false"
              appearance="fill">
              <tb-toggle-option [value]="wsCredentialsGeneratorType.AUTO">
                {{ wsCredentialsGeneratortTypeTranslationMap.get(wsCredentialsGeneratorType.AUTO) | translate }}
              </tb-toggle-option>
              <tb-toggle-option [value]="wsCredentialsGeneratorType.CUSTOM">
                {{ wsCredentialsGeneratortTypeTranslationMap.get(wsCredentialsGeneratorType.CUSTOM) | translate }}
              </tb-toggle-option>
              <tb-toggle-option [value]="wsCredentialsGeneratorType.EXISTING">
                {{ wsCredentialsGeneratortTypeTranslationMap.get(wsCredentialsGeneratorType.EXISTING) | translate }}
              </tb-toggle-option>
            </tb-toggle-header>
          </div>
          @switch (credentialsToggle.value) {
            @case (wsCredentialsGeneratorType.AUTO) {
              <mat-form-field class="mat-block">
                <mat-label translate>ws-client.connections.credentials-name</mat-label>
                <input matInput formControlName="credentialsName">
                <button mat-icon-button matSuffix
                  [matTooltip]="'action.regenerate' | translate"
                  matTooltipPosition="above"
                  (click)="regenerate('name')">
                  <mat-icon>
                    autorenew
                  </mat-icon>
                </button>
              </mat-form-field>
              <mat-form-field class="mat-block">
                <mat-label translate>mqtt-client.client-id</mat-label>
                <input matInput formControlName="clientId">
                <tb-copy-button matSuffix
                  [class.!hidden]="!connectionFormGroup.get('clientId')?.value?.length"
                  [copyText]="connectionFormGroup.get('clientId')?.value">
                </tb-copy-button>
                <button mat-icon-button matSuffix
                  [matTooltip]="'action.regenerate' | translate"
                  matTooltipPosition="above"
                  (click)="regenerate('clientId')">
                  <mat-icon>
                    autorenew
                  </mat-icon>
                </button>
              </mat-form-field>
              <mat-form-field class="mat-block">
                <mat-label translate>ws-client.connections.username</mat-label>
                <input matInput formControlName="username">
                <tb-copy-button matSuffix
                  [class.!hidden]="!connectionFormGroup.get('username')?.value?.length"
                  [copyText]="connectionFormGroup.get('username')?.value">
                </tb-copy-button>
                <button mat-icon-button matSuffix
                  [matTooltip]="'action.regenerate' | translate"
                  matTooltipPosition="above"
                  (click)="regenerate('username')" >
                  <mat-icon>
                    autorenew
                  </mat-icon>
                </button>
              </mat-form-field>
            }
            @case (wsCredentialsGeneratorType.CUSTOM) {
              <mat-form-field class="mat-block">
                <mat-label translate>mqtt-client.client-id</mat-label>
                <input matInput formControlName="clientId">
                <tb-copy-button matSuffix
                  [class.!hidden]="!connectionFormGroup.get('clientId')?.value?.length"
                  [copyText]="connectionFormGroup.get('clientId')?.value">
                </tb-copy-button>
              </mat-form-field>
              <mat-form-field class="mat-block">
                <mat-label translate>ws-client.connections.username</mat-label>
                <input matInput formControlName="username">
                <tb-copy-button matSuffix
                  [class.!hidden]="!connectionFormGroup.get('username')?.value?.length"
                  [copyText]="connectionFormGroup.get('username')?.value">
                </tb-copy-button>
              </mat-form-field>
              <mat-form-field class="mat-block">
                <mat-label translate>ws-client.connections.password</mat-label>
                <input matInput autocomplete="new-password" formControlName="password" type="password">
                <tb-toggle-password matSuffix></tb-toggle-password>
              </mat-form-field>
            }
            @case (wsCredentialsGeneratorType.EXISTING) {
              <section style="margin-bottom: 16px">
                <tb-client-credentials-autocomplete
                  formControlName="clientCredentials"
                  subscriptSizing="dynamic"
                  [entity]="connection"
                  [required]="credentialsGeneratorType === wsCredentialsGeneratorType.EXISTING"
                  [editEnabled]="true">
                </tb-client-credentials-autocomplete>
              </section>
              <mat-form-field class="mat-block">
                <mat-label translate>mqtt-client.client-id</mat-label>
                <input matInput formControlName="clientId">
                <tb-copy-button matSuffix
                  [class.!hidden]="!connectionFormGroup.get('clientId')?.value?.length"
                  [copyText]="connectionFormGroup.get('clientId')?.value">
                </tb-copy-button>
                <button mat-icon-button matSuffix
                  [matTooltip]="'action.regenerate' | translate"
                  matTooltipPosition="above"
                  (click)="regenerate('clientId')"
                  [disabled]="connectionFormGroup?.get('clientId')?.disabled">
                  <mat-icon>
                    autorenew
                  </mat-icon>
                </button>
              </mat-form-field>
              <mat-form-field class="mat-block">
                <mat-label translate>ws-client.connections.username</mat-label>
                <input matInput formControlName="username">
                <tb-copy-button matSuffix
                  [class.!hidden]="!connectionFormGroup.get('username')?.value?.length"
                  [copyText]="connectionFormGroup.get('username')?.value">
                </tb-copy-button>
              </mat-form-field>
              <mat-form-field class="mat-block" [class.!hidden]="!passwordRequired">
                <mat-label translate>ws-client.connections.password</mat-label>
                <input matInput autocomplete="new-password" formControlName="password" type="password">
                <tb-toggle-password matSuffix></tb-toggle-password>
                <button matSuffix mat-icon-button type="button" [matTooltip]="'ws-client.connections.password-warning' | translate">
                  <mat-icon style="color: #ff9a00">warning</mat-icon>
                </button>
              </mat-form-field>
            }
          }
        </section>
      </form>
    </mat-step>
    <mat-step [stepControl]="connectionAdvancedFormGroup" [optional]="true">
      <form [formGroup]="connectionAdvancedFormGroup" #conAdvancedFormGroup="ngForm" style="padding-bottom: 16px;">
        <ng-template matStepLabel>{{ 'ws-client.connections.advanced-settings' | translate}}</ng-template>
        <section class="flex flex-col gap-2">
          <section class="slider flex flex-row items-center gap-2">
            <mat-slide-toggle formControlName="clean">
              <span translate>ws-client.connections.clean-start</span>
            </mat-slide-toggle>
            <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
              [matTooltip]="'ws-client.connections.clean-start-hint' | translate"
              matTooltipPosition="right">
            info</mat-icon>
          </section>

          <section class="flex flex-row gap-4">
            <mat-form-field class="flex flex-1">
              <mat-label translate>ws-client.connections.keep-alive</mat-label>
              <input matInput formControlName="keepalive" type="number" min="0" [max]="calcMax('keepaliveUnit')">
              <mat-icon matSuffix [matTooltip]="'mqtt-client-session.keep-alive-hint' | translate">
                help
              </mat-icon>
              <mat-icon matSuffix
                        matTooltipClass="tb-error-tooltip"
                        matTooltip="{{ 'mqtt-client-session.keep-alive-required' | translate }}"
                        [class.!hidden]="!connectionAdvancedFormGroup.get('keepalive').hasError('required')"
                        class="tb-error">
                warning
              </mat-icon>
          </mat-form-field>
          <mat-form-field class="flex flex-1">
            <mat-label translate>timeunit.units</mat-label>
            <mat-select formControlName="keepaliveUnit">
              @for (unit of keepAliveTimeUnitTypes; track unit) {
                <mat-option [value]="unit">
                  {{ timeUnitTypeTranslationMap.get(unit) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </section>

        <section class="flex flex-row gap-4">
          <mat-form-field class="flex flex-1">
            <mat-label translate>ws-client.connections.connect-timeout</mat-label>
            <input matInput formControlName="connectTimeout" type="number" min="0">
            <mat-icon matSuffix [matTooltip]="'ws-client.connections.connect-timeout-hint' | translate">
              help
            </mat-icon>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'ws-client.connections.connect-timeout-required' | translate }}"
                      [class.!hidden]="!connectionAdvancedFormGroup.get('connectTimeout').hasError('required')"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
          <mat-form-field class="flex flex-1">
            <mat-label translate>timeunit.units</mat-label>
            <mat-select formControlName="connectTimeoutUnit">
              @for (unit of timeUnitTypes; track unit) {
                <mat-option [value]="unit">
                  {{ timeUnitTypeTranslationMap.get(unit) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </section>

        <section class="flex flex-row gap-4">
          <mat-form-field class="flex flex-1">
            <mat-label translate>ws-client.connections.reconnect-period</mat-label>
            <input matInput formControlName="reconnectPeriod" type="number" min="0">
            <mat-icon matSuffix [matTooltip]="'ws-client.connections.reconnect-period-hint' | translate">
              help
            </mat-icon>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'ws-client.connections.reconnect-period-required' | translate }}"
                      [class.!hidden]="!connectionAdvancedFormGroup.get('reconnectPeriod').hasError('required')"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
          <mat-form-field class="flex flex-1">
            <mat-label translate>timeunit.units</mat-label>
            <mat-select formControlName="reconnectPeriodUnit">
              @for (unit of timeUnitTypes; track unit) {
                <mat-option [value]="unit">
                  {{ timeUnitTypeTranslationMap.get(unit) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </section>

        <mat-form-field class="flex flex-1">
          <mat-label translate>ws-client.connections.mqtt-version</mat-label>
          <mat-select formControlName="protocolVersion">
            @for (mqttVersion of mqttVersions; track mqttVersion) {
              <mat-option [value]="mqttVersion.value">
                {{ mqttVersion.name }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>

        <section class="flex flex-col" formGroupName="properties">
          <section class="flex flex-row gap-4">
            <mat-form-field class="flex flex-1">
              <mat-label translate>ws-client.connections.session-expiry-interval</mat-label>
              <input matInput formControlName="sessionExpiryInterval" type="number" min="0" [max]="calcMax('sessionExpiryIntervalUnit')">
          </mat-form-field>
          <mat-form-field class="flex flex-1">
            <mat-label translate>timeunit.units</mat-label>
            <mat-select formControlName="sessionExpiryIntervalUnit">
              @for (unit of timeUnitTypes; track unit) {
                <mat-option [value]="unit">
                  {{ timeUnitTypeTranslationMap.get(unit) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </section>
        <section class="flex flex-row gap-4">
          <mat-form-field class="flex flex-1">
            <mat-label translate>ws-client.connections.maximum-packet-size</mat-label>
            <input matInput formControlName="maximumPacketSize" type="number" min="0" [max]="calcMax('maximumPacketSizeUnit')">
          <mat-icon matSuffix [matTooltip]="'ws-client.connections.maximum-packet-size-hint' | translate">
            help
          </mat-icon>
        </mat-form-field>
        <mat-form-field class="flex flex-1">
          <mat-label translate>timeunit.units</mat-label>
          <mat-select formControlName="maximumPacketSizeUnit">
            @for (unit of dataSizeUnitTypes; track unit) {
              <mat-option [value]="unit">
                {{ dataSizeUnitTypeTranslationMap.get(unit) }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
      </section>
      <mat-form-field class="mat-block">
        <mat-label translate>ws-client.connections.topic-alias-maximum</mat-label>
        <input matInput formControlName="topicAliasMaximum" type="number" min="0" max="65535">
      <mat-icon matSuffix [matTooltip]="'ws-client.connections.topic-alias-maximum-hint' | translate">
        help
      </mat-icon>
    </mat-form-field>
    <mat-form-field class="mat-block">
      <mat-label translate>ws-client.connections.receive-maximum</mat-label>
      <input matInput formControlName="receiveMaximum" type="number" min="1" max="65535">
    <mat-icon matSuffix [matTooltip]="'ws-client.connections.receive-maximum-hint' | translate">
      help
    </mat-icon>
  </mat-form-field>
  <section class="slider flex flex-row items-center gap-2">
    <mat-slide-toggle formControlName="requestResponseInfo">
      <span translate>ws-client.connections.request-response-information</span>
    </mat-slide-toggle>
    <mat-icon class="tb-mat-20" style="color: rgba(0,0,0,0.18)"
      [matTooltip]="'ws-client.connections.request-response-information-hint' | translate"
      matTooltipPosition="right">
    info</mat-icon>
  </section>
</section>
</section>
</form>
</mat-step>
<mat-step [stepControl]="lastWillFormGroup" [optional]="true">
  <ng-template matStepLabel>{{ 'ws-client.last-will.last-will' | translate }}</ng-template>
  <form [formGroup]="lastWillFormGroup" #lwFormGroup="ngForm">
    <tb-last-will formControlName="lastWillMsg"
      [mqttVersion]="mqttVersion"
      [entity]="connection">
    </tb-last-will>
  </form>
</mat-step>
<mat-step [stepControl]="userPropertiesFormGroup" [optional]="true">
  <ng-template matStepLabel>{{ 'retained-message.user-properties' | translate }}</ng-template>
  <form [formGroup]="userPropertiesFormGroup" #upFormGroup="ngForm">
    <tb-user-properties formControlName="userProperties"
      [mqttVersion]="mqttVersion"
      [entity]="connection?.configuration?.userProperties">
    </tb-user-properties>
  </form>
</mat-step>
</mat-horizontal-stepper>
</div>
<div mat-dialog-actions class="tb-dialog-actions">
  <div class="dialog-actions-row">
    @if (selectedIndex > 0) {
      <button mat-stroked-button
        [disabled]="(isLoading$ | async)"
      (click)="previousStep()">{{ 'action.back' | translate }}</button>
    }
    <span class="flex flex-1"></span>
    @if (showNext) {
      <button mat-stroked-button
        color="primary"
        [disabled]="(isLoading$ | async)"
      (click)="nextStep()">{{ 'action.next-with-label' | translate:{label: (getFormLabel(this.selectedIndex+1) | translate)} }}</button>
    }
  </div>
  <mat-divider style="width: 100%"></mat-divider>
  <div class="dialog-actions-row">
    <button mat-button
      [disabled]="(isLoading$ | async)"
    (click)="cancel()">{{ 'action.cancel' | translate }}</button>
    <button mat-raised-button
      [disabled]="(isLoading$ | async) || conFormGroup?.invalid || conAdvancedFormGroup?.invalid || lwFormGroup?.invalid || upFormGroup?.invalid"
      color="primary"
    (click)="save()">{{ 'ws-client.connections.connect' | translate }}</button>
  </div>
</div>
